package com.example.meetingroomnew.controll;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.meetingroomnew.Dao.MeetingRoomDao;
import com.example.meetingroomnew.domain.DataClass;
import com.example.meetingroomnew.domain.MeetingRoom;
import com.example.meetingroomnew.domain.Reservation;
import com.example.meetingroomnew.service.MeetingRoomService;
import com.example.meetingroomnew.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
@CrossOrigin(origins = {"*","null"})
@RestController
@RequestMapping
public class meetingRoomController {
    @Autowired
   private MeetingRoomService meetingRoomService;
    @Autowired
    private RequestService requestService;
    @Autowired
    MeetingRoomDao meetingRoomDao;

    @GetMapping("/mRoom")
    public DataClass selectAll(){
        List<MeetingRoom> meetingRooms = meetingRoomService.selectAll();
        DataClass dataClass=new DataClass();
        DataClass<MeetingRoom> dataClass1=new DataClass<MeetingRoom>();
        dataClass1.setData(meetingRooms);
        return dataClass1;
    }
    @PostMapping("/search")
    public DataClass<MeetingRoom>selectBymeetingName(@RequestBody MeetingRoom meetingRoom){
        List<MeetingRoom> meetingRooms = meetingRoomService.selectByname(meetingRoom.getName(), meetingRoom.getRoomNumber());
        DataClass<MeetingRoom> dataClass1=new DataClass<MeetingRoom>();
        dataClass1.setData(meetingRooms);
        return dataClass1;

    }

    @PostMapping("/request")
    public DataClass<MeetingRoom> selectAvailiableMeetingroom(@RequestBody Reservation reservation){
        Date date1=reservation.getStartTime();
        Date date2=reservation.getEndTime();
        int num=reservation.getAttendees();
        System.out.println("num="+num);
        List<Reservation> reservations = requestService.selectByTime(date1, date2);
        LambdaQueryWrapper<MeetingRoom> qw=new LambdaQueryWrapper<MeetingRoom>();
//        qw为了排除在时间段中的房间
        LambdaQueryWrapper<MeetingRoom> qw2=new LambdaQueryWrapper<MeetingRoom>();
//        qw2为了排除房间人数不合适的人
        for(int i=0;i<reservations.size();i++){
            qw.ne(MeetingRoom::getId,reservations.get(i).getRoomId());
        }
        qw.ge(MeetingRoom::getCapacity,num);
        List<MeetingRoom> meetingRooms = meetingRoomDao.selectList(qw);
        DataClass<MeetingRoom> dataClass1=new DataClass<MeetingRoom>();
        dataClass1.setData(meetingRooms);
        System.out.println(reservation);
        return dataClass1;

    }
    @PostMapping("/create")
    public int InsertMeetingRoom(@RequestBody MeetingRoom meetingRoom){
        boolean flag = meetingRoomService.InsertmeetingRoom(meetingRoom);
        if (flag){
            return 1;
        }else {
            return 0;
        }
    }
    @PostMapping("/deleteroom")
    public int Delete(@RequestBody MeetingRoom meetingRoom){

        int id=meetingRoom.getId();
        boolean flag = meetingRoomService.deletemeetingRoom(id);
//        1为成功，2为失败
        if(flag){

            return 1;
        }else {
            return 0;
        }

    }
    @PostMapping("/reviseroom")
    public int update(@RequestBody MeetingRoom meetingRoom){

        System.out.println(meetingRoom);
        boolean flag = meetingRoomService.update(meetingRoom);
        if (flag==true){
            return 1;
        }else {
            return 0;
        }
    }




}
